package br.ucb.ProjetoFinal.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 * source : www.javabeat.net
 */
public class ValidatorNis implements Validator{

    public void validate(FacesContext facesContext, UIComponent arg1, Object value) throws ValidatorException {

        String cod = (String) value;

        Pattern mascara = Pattern.compile("[0-9]{10}[-][0-9]");
        Matcher match = mascara.matcher(cod);

        if (!match.matches() && !cod.equals("")){
                FacesMessage message = new FacesMessage();
                message.setSummary("NIS - Codigo inválido. Ele deve estar no formato xxxxxxxxxx-x'");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);                
        }
    }
}